﻿using System;
using System.Reflection;
using System.Configuration;
using OnlineManagement.IDAL;
using OnlineManagement.Common;
namespace OnlineManagement.DALFactory
{
    /// <summary>
    /// 抽象工厂模式创建DAL。
    /// web.config 需要加入配置：(利用工厂模式+反射机制+缓存机制,实现动态创建不同的数据层对象接口) 
    /// DataCache类在导出代码的文件夹里
    /// <appSettings> 
    /// <add key="DAL" value="OnlineManagement.SQLServerDAL.Dal" /> (这里的命名空间根据实际情况更改为自己项目的命名空间)
    /// </appSettings> 
    /// </summary>
    public sealed class DataAccess
    {
        private static readonly string AssemblyPath = ConfigurationManager.AppSettings["DAL"];
        /// <summary>
        /// 创建对象或从缓存获取 
        /// </summary>
        public static object CreateObject(string AssemblyPath, string ClassNamespace)
        {
            object objType = DataCache.GetCache(ClassNamespace);//从缓存读取
            if (objType == null)
            {
                try
                {
                    objType = Assembly.Load(AssemblyPath).CreateInstance(ClassNamespace);//反射创建
                    DataCache.SetCache(ClassNamespace, objType);// 写入缓存
                }
                catch
                { }
            }
            return objType;
        }
        /// <summary>
        /// 创建数据层接口
        /// </summary>
        //public static t Create(string ClassName)
        //{
        //string ClassNamespace = AssemblyPath +"."+ ClassName;
        //object objType = CreateObject(AssemblyPath, ClassNamespace);
        //return (t)objType;
        //}
        /// <summary>
        /// 创建TempRegist数据层接口。
        /// </summary>
        public static OnlineManagement.IDAL.ITempRegist CreateTempRegist()
        {

            string ClassNamespace = AssemblyPath + ".TempRegist";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (OnlineManagement.IDAL.ITempRegist)objType;
        }

        /// <summary>
        /// 创建数据层接口
        /// </summary>
        //public static t Create(string ClassName)
        //{
        //string ClassNamespace = AssemblyPath +"."+ ClassName;
        //object objType = CreateObject(AssemblyPath, ClassNamespace);
        //return (t)objType;
        //}
        /// <summary>
        /// 创建ConstitutionFodder数据层接口。
        /// </summary>
        public static OnlineManagement.IDAL.IConstitutionFodder CreateConstitutionFodder()
        {

            string ClassNamespace = AssemblyPath + ".Dal.ConstitutionFodder";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (OnlineManagement.IDAL.IConstitutionFodder)objType;
        }


        /// <summary>
        /// 创建Feeding数据层接口。
        /// </summary>
        public static OnlineManagement.IDAL.IFeeding CreateFeeding()
        {

            string ClassNamespace = AssemblyPath + ".Dal.Feeding";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (OnlineManagement.IDAL.IFeeding)objType;
        }


        /// <summary>
        /// 创建Fodder数据层接口。
        /// </summary>
        public static OnlineManagement.IDAL.IFodder CreateFodder()
        {

            string ClassNamespace = AssemblyPath + ".Dal.Fodder";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (OnlineManagement.IDAL.IFodder)objType;
        }


        /// <summary>
        /// 创建GrowPhase数据层接口。
        /// </summary>
        public static OnlineManagement.IDAL.IGrowPhase CreateGrowPhase()
        {

            string ClassNamespace = AssemblyPath + ".Dal.GrowPhase";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (OnlineManagement.IDAL.IGrowPhase)objType;
        }


        /// <summary>
        /// 创建Hoggery数据层接口。
        /// </summary>
        public static OnlineManagement.IDAL.IHoggery CreateHoggery()
        {

            string ClassNamespace = AssemblyPath + ".Dal.Hoggery";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (OnlineManagement.IDAL.IHoggery)objType;
        }


        /// <summary>
        /// 创建HybridizationRecord数据层接口。
        /// </summary>
        public static OnlineManagement.IDAL.IHybridizationRecord CreateHybridizationRecord()
        {

            string ClassNamespace = AssemblyPath + ".Dal.HybridizationRecord";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (OnlineManagement.IDAL.IHybridizationRecord)objType;
        }

        /// <summary>
        /// 创建FaQingRecord数据层接口。
        /// </summary>
        /// <returns></returns>
        public static OnlineManagement.IDAL.IFaQingRecord CreateFaQingRecord()
        {
            string classNamespace = AssemblyPath + ".FaQingRecord";
            object obj = CreateObject(AssemblyPath, classNamespace);
            return obj as OnlineManagement.IDAL.IFaQingRecord;
        }

        /// <summary>
        /// 创建PigBreed数据层接口。
        /// </summary>
        public static OnlineManagement.IDAL.IPigBreed CreatePigBreed()
        {

            string ClassNamespace = AssemblyPath + ".Dal.PigBreed";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (OnlineManagement.IDAL.IPigBreed)objType;
        }


        /// <summary>
        /// 创建Piglets数据层接口。
        /// </summary>
        public static OnlineManagement.IDAL.IPiglets CreatePiglets()
        {

            string ClassNamespace = AssemblyPath + ".Dal.Piglets";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (OnlineManagement.IDAL.IPiglets)objType;
        }


        /// <summary>
        /// 创建PigPen数据层接口。
        /// </summary>
        public static OnlineManagement.IDAL.IPigPen CreatePigPen()
        {

            string ClassNamespace = AssemblyPath + ".Dal.PigPen";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (OnlineManagement.IDAL.IPigPen)objType;
        }


        /// <summary>
        /// 创建PigSource数据层接口。
        /// </summary>
        public static OnlineManagement.IDAL.IPigSource CreatePigSource()
        {

            string ClassNamespace = AssemblyPath + ".Dal.PigSource";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (OnlineManagement.IDAL.IPigSource)objType;
        }


        /// <summary>
        /// 创建Porks数据层接口。
        /// </summary>
        public static OnlineManagement.IDAL.IPorks CreatePorks()
        {

            string ClassNamespace = AssemblyPath + ".Dal.Porks";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (OnlineManagement.IDAL.IPorks)objType;
        }


        /// <summary>
        /// 创建PropagateRecord数据层接口。
        /// </summary>
        public static OnlineManagement.IDAL.IPropagateRecord CreatePropagateRecord()
        {

            string ClassNamespace = AssemblyPath + ".Dal.PropagateRecord";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (OnlineManagement.IDAL.IPropagateRecord)objType;
        }


        /// <summary>
        /// 创建PropagateState数据层接口。
        /// </summary>
        public static OnlineManagement.IDAL.IPropagateState CreatePropagateState()
        {

            string ClassNamespace = AssemblyPath + ".Dal.PropagateState";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (OnlineManagement.IDAL.IPropagateState)objType;
        }


        /// <summary>
        /// 创建Fodder Purchase数据层接口。
        /// </summary>
        public static OnlineManagement.IDAL.IFodderPurchase CreateFodderPurchase()
        {

            string ClassNamespace = AssemblyPath + ".Dal.FodderPurchase";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (OnlineManagement.IDAL.IFodderPurchase)objType;
        }


        /// <summary>
        /// 创建Sow数据层接口。
        /// </summary>
        public static OnlineManagement.IDAL.ISow CreateSow()
        {

            string ClassNamespace = AssemblyPath + ".Dal.Sow";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (OnlineManagement.IDAL.ISow)objType;
        }
        public static IHog CreateHog()
        {
            string nameSpace = AssemblyPath + ".Hog";
            object obj = CreateObject(AssemblyPath, nameSpace);
            return (IHog)obj;
        }

        public static IHogSell CreateHogSell()
        {
            string nameSpace = AssemblyPath + ".Dal.HogSell";
            object obj = CreateObject(AssemblyPath, nameSpace);
            return (IHogSell)obj;
        }

        public static IHogPurchase CreateHogPurchase() {
            string nameSpace = AssemblyPath + ".Dal.HogPurchase";
            object obj = CreateObject(AssemblyPath, nameSpace);
            return (IHogPurchase)obj;
        }



        public static IProcedureHandle CreateProcedureHandle() {
            string nameSpace = AssemblyPath + ".ProcedureHandle";
            object obj = CreateObject(AssemblyPath, nameSpace);
            return (IProcedureHandle)obj;
        }
        /// <summary>
        /// 创建乳猪断奶登记 数据接口层
        /// </summary>
        /// <returns></returns>
        public static IWeanedRecord CreateWeanedRecord() {
            string nameSpace = AssemblyPath + ".WeanedRecord";
            object obj = CreateObject(AssemblyPath, nameSpace);
            return (IWeanedRecord)obj;
        }
    }
}